package hbxz.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import hbxz.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yhb
 * @date 2021/6/7
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 基于http
     * @author yhb
     * @date 2021/6/7
     */
    @GetMapping("/buy/http")
    private String bugGoodHttp( HttpServletRequest request){
        log.info("订单服务{} ---- ",request.getRequestURI());
        String forObject = restTemplate.getForObject("http://localhost:8082/credit/add", String.class);
        log.info("调用积分服务成功返回值{}",forObject);
        return "ok";
    }

    /**
     * 基于http
     * @author yhb
     * @date 2021/6/7
     */
    @GetMapping("/buy/ribbon")
    private String bugGoodRibbon( HttpServletRequest request){
        log.info("订单服务{} ---- ",request.getRequestURI());
        String forObject = restTemplate.getForObject("http://credit-service/credit/add", String.class);
        log.info("调用积分服务成功返回值{}",forObject);
        return "ok";
    }



    @Autowired
    private OrderService orderService;
    /**
     * 基于feign
     * @author yhb
     * @date 2021/6/7
     */
    @GetMapping("/create")
    private String createOrder( HttpServletRequest request){
        log.info("订单服务{} ---- {}",request.getRequestURI());
        orderService.createOrder();
        return "ok";
    }

    @GetMapping(value = "/hello")
    @SentinelResource("hello")
    public String hello() {
        return "Hello Sentinel";
    }


}
